send_loop_times = 50
wait_timeout = "10"

socket_cmd = [
    {
        "cmd": 'AT+CIPMUX=0',
        "match": 'AT\+CIPMUX=0\s{4}OK\s{2}'
    },
    {
        "cmd": 'AT+CIPSSL=0',
        "match": 'AT\+CIPSSL=0\s{4}OK\s{2}'
    },
    {
        "cmd": 'AT+CIPMODE=0',
        "match": 'AT\+CIPMODE=0\s{4}OK\s{2}'
    },
    {
        "cmd": 'AT+CIPHEAD=1',
        "match": 'AT\+CIPHEAD=1\s{4}OK\s{2}'
    },
    {
        "cmd": 'AT+CIPSTART=TCP,"airtest.openluat.com",2901',
        "match": 'AT\+CIPSTART=TCP,"airtest.openluat.com",2901\s{4}OK\s{4}CONNECT OK\s{2}',
        "wait": b'CONNECT OK',
        "timeout": wait_timeout
    },
    # {
    #     "cmd": 'AT+CIPSEND=1360',
    #     "match": 'AT\+CIPSEND=1360\s{4}> ',
    #     "wait": b'> ',
    #     "timeout": wait_timeout
    # },
    # {
    #     "cmd": '6356903774146048357916340540664406481606106861097114067014687553992233630919427860815055116106340161071101370643328757209511462123876015527920571469012350790962324909904604915081920313289107414899025246233649724138500139833948244174832087569154375163892559553388608900869139013241238748571319254724658947195218611117462346792382732690529020286217199922826418095176155629452491041405783235655758568719143850639040688663198577856799163334390854693270436053594784362648565386712467707657902613456300002122157708934668571586578521814137937715121976898547337739214732659542545881377007981541149919965038434080106067383701716886849656056365607457872944404852763201960017567650140575551337448189990368941633888423207641542489051537306359869322507950786574524446252241294917068473710597198877716906081035795463815383311696560434277107030978605587863913525977494952912255609289110260073251722233922764951650015103969914702010733996052098480893129565506121689464927270864924062877729478363690123893784944792170985679932446535649583245507839148849971460064330685731969354589146488410095333517887409231134644990113211339175027815774943806516754553155791387913450448444777676968120115900406069751215557189054606560125353052809866198818931022291047062508880934599079866222285706557335777524801967505497293407394573502748871896675205815467754711286251210157671505046018810901',
    #     "match": '\d{1360}\s{2}SEND OK\s{2}\+IPD,1360:\d{1360}\s{2}',
    #     "wait": b'+IPD,1360:6356903774146048357916340540664406481606106861097114067014687553992233630919427860815055116106340161071101370643328757209511462123876015527920571469012350790962324909904604915081920313289107414899025246233649724138500139833948244174832087569154375163892559553388608900869139013241238748571319254724658947195218611117462346792382732690529020286217199922826418095176155629452491041405783235655758568719143850639040688663198577856799163334390854693270436053594784362648565386712467707657902613456300002122157708934668571586578521814137937715121976898547337739214732659542545881377007981541149919965038434080106067383701716886849656056365607457872944404852763201960017567650140575551337448189990368941633888423207641542489051537306359869322507950786574524446252241294917068473710597198877716906081035795463815383311696560434277107030978605587863913525977494952912255609289110260073251722233922764951650015103969914702010733996052098480893129565506121689464927270864924062877729478363690123893784944792170985679932446535649583245507839148849971460064330685731969354589146488410095333517887409231134644990113211339175027815774943806516754553155791387913450448444777676968120115900406069751215557189054606560125353052809866198818931022291047062508880934599079866222285706557335777524801967505497293407394573502748871896675205815467754711286251210157671505046018810901\r\n',
    #     "timeout": wait_timeout
    # },
    # {
    #     "cmd": 'AT+CIPSEND=1360\r\n6356903774146048357916340540664406481606106861097114067014687553992233630919427860815055116106340161071101370643328757209511462123876015527920571469012350790962324909904604915081920313289107414899025246233649724138500139833948244174832087569154375163892559553388608900869139013241238748571319254724658947195218611117462346792382732690529020286217199922826418095176155629452491041405783235655758568719143850639040688663198577856799163334390854693270436053594784362648565386712467707657902613456300002122157708934668571586578521814137937715121976898547337739214732659542545881377007981541149919965038434080106067383701716886849656056365607457872944404852763201960017567650140575551337448189990368941633888423207641542489051537306359869322507950786574524446252241294917068473710597198877716906081035795463815383311696560434277107030978605587863913525977494952912255609289110260073251722233922764951650015103969914702010733996052098480893129565506121689464927270864924062877729478363690123893784944792170985679932446535649583245507839148849971460064330685731969354589146488410095333517887409231134644990113211339175027815774943806516754553155791387913450448444777676968120115900406069751215557189054606560125353052809866198818931022291047062508880934599079866222285706557335777524801967505497293407394573502748871896675205815467754711286251210157671505046018810901',
    #     "match": 'AT\+CIPSEND=1360\s{4}> \d{1360}\s{2}SEND OK\s{2}\+IPD,1360:\d{1360}\s{2}',
    #     "enter": False,
    #     "loop": send_loop_times,
    #     "wait": b'+IPD,1360:6356903774146048357916340540664406481606106861097114067014687553992233630919427860815055116106340161071101370643328757209511462123876015527920571469012350790962324909904604915081920313289107414899025246233649724138500139833948244174832087569154375163892559553388608900869139013241238748571319254724658947195218611117462346792382732690529020286217199922826418095176155629452491041405783235655758568719143850639040688663198577856799163334390854693270436053594784362648565386712467707657902613456300002122157708934668571586578521814137937715121976898547337739214732659542545881377007981541149919965038434080106067383701716886849656056365607457872944404852763201960017567650140575551337448189990368941633888423207641542489051537306359869322507950786574524446252241294917068473710597198877716906081035795463815383311696560434277107030978605587863913525977494952912255609289110260073251722233922764951650015103969914702010733996052098480893129565506121689464927270864924062877729478363690123893784944792170985679932446535649583245507839148849971460064330685731969354589146488410095333517887409231134644990113211339175027815774943806516754553155791387913450448444777676968120115900406069751215557189054606560125353052809866198818931022291047062508880934599079866222285706557335777524801967505497293407394573502748871896675205815467754711286251210157671505046018810901\r\n',
    #     "timeout": wait_timeout
    # },
    {
        "cmd": 'AT+CIPSTATUS',
        "match": 'AT\+CIPSTATUS\s{4}OK\s{4}STATE: CONNECT OK\s{2}'
    },
    {
        "cmd": 'AT+CIPSHUT',
        "match": 'AT\+CIPSHUT\r\n\r\nSHUT OK\r\n',
        "wait": b'SHUT OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSTATUS',
        "match": 'AT\+CIPSTATUS\s{4}OK\s{4}STATE: IP INITIAL\s{2}'
    },
    {
        "cmd": 'AT+CIPSTART="UDP","airtest.openluat.com",2901',
        "match": 'AT\+CIPSTART="UDP","airtest.openluat.com",2901\s{4}OK\s{4}CONNECT OK\s{2}',
        "wait": b'CONNECT OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSEND=1024\r\n8670801493665826410525123648623748213706262358528091262383557825186070531029949883212175654560750487217936289268178850814160786503979031223304780338621092575442623074532408923343441956361992859968115851517331898932628028551660787962483550933034115824653567299480922583701650909028331489711778790055590230107077032817486853638497339432406653094399487923338423779074409610457678941790728269938349260919542459529169464032088718797227516741159653951923050796439115203845489828171687762397983094312042998428209386402607274578995149469168233248655141928457922243550447294738782065667459438935895866238813146861150708864616899304902375274568017564605092267310418610077675135649098300102754857698388139059402682076072360061107548154406295006958014079095644945678530927389317352239309754297622467861438845698138462951674198501053992894857942859414132760456020298704398176922922599197208799466276910694869918150622143704766170718559649442563245502070327978810753522318677870990983699453832395535922509140945190436759031635474820345326',
        "match": 'AT\+CIPSEND=1024\s{4}> \d{1024}\s{2}SEND OK\s{2}\+IPD,1024:\d{1024}\s{2}',
        "enter": False,
        "loop": send_loop_times,
        "wait": b'+IPD,1024:8670801493665826410525123648623748213706262358528091262383557825186070531029949883212175654560750487217936289268178850814160786503979031223304780338621092575442623074532408923343441956361992859968115851517331898932628028551660787962483550933034115824653567299480922583701650909028331489711778790055590230107077032817486853638497339432406653094399487923338423779074409610457678941790728269938349260919542459529169464032088718797227516741159653951923050796439115203845489828171687762397983094312042998428209386402607274578995149469168233248655141928457922243550447294738782065667459438935895866238813146861150708864616899304902375274568017564605092267310418610077675135649098300102754857698388139059402682076072360061107548154406295006958014079095644945678530927389317352239309754297622467861438845698138462951674198501053992894857942859414132760456020298704398176922922599197208799466276910694869918150622143704766170718559649442563245502070327978810753522318677870990983699453832395535922509140945190436759031635474820345326\r\n',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSTATUS',
        "match": 'AT\+CIPSTATUS\s{4}OK\s{4}STATE: CONNECT OK\s{2}'
    },
    {
        "cmd": 'AT+CIPSHUT',
        "match": 'AT\+CIPSHUT\r\n\r\nSHUT OK\r\n',
        "wait": b'SHUT OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSTATUS',
        "match": 'AT\+CIPSTATUS\s{4}OK\s{4}STATE: IP INITIAL\s{2}'
    },
    {
        "cmd": 'AT+FSDEL="ca.crt"',
        "match": '.*',
        "wait": b'OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+FSDEL="client.crt"',
        "match": '.*',
        "wait": b'OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+FSDEL="client.key"',
        "match": '.*',
        "wait": b'OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+FSCREATE=ca.crt',
        "match": 'AT\+FSCREATE=ca\.crt\s{4}OK\s{2}',
        "wait": b'OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+FSWRITE=ca.crt,0,1350,5',
        "match": 'AT\+FSWRITE=ca\.crt,0,1350,5\s{4}>',
        "wait": b'>',
        "timeout": wait_timeout
    },
    {
        "cmd": open("certs/tcp/ca.crt", "rb").read().decode(encoding="utf-8").replace("\r\n", "\n"),
        "match": '\s{2}OK\s{2}',
        "enter": False,
        "wait": b'OK',
        "timeout": wait_timeout
    },
    {
        "cmd": "AT+FSREAD=ca.crt,0,1350,0",
        "match": '.*',
        "wait": b'OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+FSCREATE=client.crt',
        "match": 'AT\+FSCREATE=client\.crt\s{4}OK\s{2}',
        "wait": b'OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+FSWRITE=client.crt,0,1188,100',
        "match": 'AT\+FSWRITE=client\.crt,0,1188,100\s{4}>',
        "wait": b'>',
        "timeout": wait_timeout
    },
    {
        "cmd": open("certs/tcp/client.crt", "rb").read().decode(encoding="utf-8").replace("\r\n", "\n"),
        "match": '\s{2}OK\s{2}',
        "enter": False,
        "wait": b'OK',
        "timeout": wait_timeout
    },
    {
        "cmd": "AT+FSREAD=client.crt,0,1188,0",
        "match": '.*',
        "wait": b'OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+FSCREATE=client.key',
        "match": 'AT\+FSCREATE=client\.key\s{4}OK\s{2}',
        "wait": b'OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+FSWRITE=client.key,0,1675,5',
        "match": 'AT\+FSWRITE=client\.key,0,1675,5\s{4}>',
        "wait": b'>',
        "timeout": wait_timeout
    },
    {
        "cmd": open("certs/tcp/client.key", "rb").read().decode(encoding="utf-8").replace("\r\n", "\n"),
        "match": '\s{2}OK\s{2}',
        "enter": False,
        "wait": b'OK',
        "timeout": wait_timeout
    },
    {
        "cmd": "AT+FSREAD=client.key,0,1675,0",
        "match": '.*',
        "wait": b'OK',
        "timeout": wait_timeout
    },
    {
        "cmd": "AT+CIPSSL=1",
        "match": 'AT\+CIPSSL=1\s{4}OK\s{2}'
    },
    {
        "cmd": 'AT+SSLCFG="seclevel",0,2',
        "match": 'AT\+SSLCFG="seclevel",0,2\s{4}OK\s{2}'
    },
    {
        "cmd": 'AT+SSLCFG="cacert",0,"ca.crt"',
        "match": 'AT\+SSLCFG="cacert",0,"ca\.crt"\s{4}OK\s{2}'
    },
    {
        "cmd": 'AT+SSLCFG="clientcert",0,"client.crt"',
        "match": 'AT\+SSLCFG="clientcert",0,"client\.crt"\s{4}OK\s{2}'
    },
    {
        "cmd": 'AT+SSLCFG="clientkey",0,"client.key"',
        "match": 'AT\+SSLCFG="clientkey",0,"client\.key"\s{4}OK\s{2}'
    },
    {
        "cmd": 'AT+CIPSTART="TCP","airtest.openluat.com",443',
        "match": 'AT\+CIPSTART="TCP","airtest.openluat.com",443\s{4}OK\s{4}CONNECT OK\s{2}',
        "wait": b'CONNECT OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSEND=46\r\nGET / HTTP/1.1\r\nHost: airtest.openluat.com\r\n\r\n',
        "match": '.*',
        "enter": False,
        # "wait": b'+IPD,436:5069056007465973089504432489618783455450682379939056882006381175133565259567328826928464646373706648262297847560261790175009413501879465498586755649184308675175005981323620666741813321494232245089875028327438384300669653111394490443355559116715333435378035886161827988117589311545061802533971097974028809823832967965744412178670210713260314389341281025734511174305007804809046675248533085992617012968209640429012285645468580465709858951\r\n',
        # "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSTATUS',
        "match": 'AT\+CIPSTATUS\s{4}OK\s{4}STATE: CONNECT OK\s{2}'
    },
    {
        "cmd": 'AT+CIPSHUT',
        "match": 'AT\+CIPSHUT\r\n\r\nSHUT OK\r\n',
        "wait": b'SHUT OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSTATUS',
        "match": 'AT\+CIPSTATUS\s{4}OK\s{4}STATE: IP INITIAL\s{2}'
    },
    {
        "cmd": 'AT+CIPMODE=0',
        "match": 'AT\+CIPMODE=0\s{4}OK\s{2}'
    },
    {
        "cmd": 'AT+CIPMUX=1',
        "match": 'AT\+CIPMUX=1\s{4}OK\s{2}'
    },

    {
        "cmd": 'AT+CIPSSL=0',
        "match": 'AT\+CIPSSL=0\s{4}OK\s{2}'
    },
    {
        "cmd": 'AT+CIPSTART=0,TCP,"airtest.openluat.com",2901',
        "match": 'AT\+CIPSTART=0,TCP,"airtest.openluat.com",2901\s{4}OK\s{4}0, CONNECT OK\s{2}',
        "wait": b'CONNECT OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSTART=1,TCP,"airtest.openluat.com",2901',
        "match": 'AT\+CIPSTART=1,TCP,"airtest.openluat.com",2901\s{4}OK\s{4}1, CONNECT OK\s{2}',
        "wait": b'CONNECT OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSTART=2,TCP,"airtest.openluat.com",2901',
        "match": 'AT\+CIPSTART=2,TCP,"airtest.openluat.com",2901\s{4}OK\s{4}2, CONNECT OK\s{2}',
        "wait": b'CONNECT OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSTART=3,"UDP","airtest.openluat.com",2901',
        "match": 'AT\+CIPSTART=3,"UDP","airtest.openluat.com",2901\s{4}OK\s{4}3, CONNECT OK\s{2}',
        "wait": b'CONNECT OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSTART=4,"UDP","airtest.openluat.com",2901',
        "match": 'AT\+CIPSTART=4,"UDP","airtest.openluat.com",2901\s{4}OK\s{4}4, CONNECT OK\s{2}',
        "wait": b'CONNECT OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSTART=5,"UDP","airtest.openluat.com",2901',
        "match": 'AT\+CIPSTART=5,"UDP","airtest.openluat.com",2901\s{4}OK\s{4}5, CONNECT OK\s{2}',
        "wait": b'CONNECT OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSEND=0,100\r\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890',
        "match": 'AT\+CIPSEND=0,100\s{4}> 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\s{2}0, SEND OK\s{4}\+RECEIVE,0,100:\s{2}1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\s{2}',
        "enter": False,
        "loop": send_loop_times,
        "wait": b'SEND OK\r\n\r\n+RECEIVE,0,100:\r\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\r\n',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSEND=1,100\r\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890',
        "match": 'AT\+CIPSEND=1,100\s{4}> 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\s{2}1, SEND OK\s{4}\+RECEIVE,1,100:\s{2}1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\s{2}',
        "enter": False,
        "loop": send_loop_times,
        "wait": b'SEND OK\r\n\r\n+RECEIVE,1,100:\r\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\r\n',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSEND=2,100\r\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890',
        "match": 'AT\+CIPSEND=2,100\s{4}> 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\s{2}2, SEND OK\s{4}\+RECEIVE,2,100:\s{2}1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\s{2}',
        "enter": False,
        "loop": send_loop_times,
        "wait": b'SEND OK\r\n\r\n+RECEIVE,2,100:\r\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\r\n',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSEND=3,100\r\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890',
        "match": 'AT\+CIPSEND=3,100\s{4}> 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\s{2}3, SEND OK\s{4}\+RECEIVE,3,100:\s{2}1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\s{2}',
        "enter": False,
        "loop": send_loop_times,
        "wait": b'SEND OK\r\n\r\n+RECEIVE,3,100:\r\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\r\n',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSEND=4,100\r\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890',
        "match": 'AT\+CIPSEND=4,100\s{4}> 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\s{2}4, SEND OK\s{4}\+RECEIVE,4,100:\s{2}1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\s{2}',
        "enter": False,
        "loop": send_loop_times,
        "wait": b'SEND OK\r\n\r\n+RECEIVE,4,100:\r\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\r\n',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPSEND=5,100\r\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890',
        "match": 'AT\+CIPSEND=5,100\s{4}> 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\s{2}5, SEND OK\s{4}\+RECEIVE,5,100:\s{2}1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\s{2}',
        "enter": False,
        "loop": send_loop_times,
        "wait": b'SEND OK\r\n\r\n+RECEIVE,5,100:\r\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\r\n',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPCLOSE=0',
        "match": 'AT\+CIPCLOSE=0\s{4}0, CLOSE OK\s{2}'
    },
    {
        "cmd": 'AT+CIPCLOSE=1',
        "match": 'AT\+CIPCLOSE=1\s{4}1, CLOSE OK\s{2}'
    },
    {
        "cmd": 'AT+CIPCLOSE=2',
        "match": 'AT\+CIPCLOSE=2\s{4}2, CLOSE OK\s{2}'
    },
    {
        "cmd": 'AT+CIPCLOSE=3',
        "match": 'AT\+CIPCLOSE=3\s{4}3, CLOSE OK\s{2}'
    },
    {
        "cmd": 'AT+CIPCLOSE=4',
        "match": 'AT\+CIPCLOSE=4\s{4}4, CLOSE OK\s{2}'
    },
    {
        "cmd": 'AT+CIPCLOSE=5',
        "match": 'AT\+CIPCLOSE=5\s{4}5, CLOSE OK\s{2}'
    },
    {
        "cmd": 'AT+CIPSHUT',
        "match": 'AT\+CIPSHUT\r\n\r\nSHUT OK\r\n',
        "wait": b'SHUT OK',
        "timeout": wait_timeout
    },
    {
        "cmd": 'AT+CIPMUX=0',
        "match": 'AT\+CIPMUX=0\s{4}OK\s{2}'
    },
]
